package com.answer.petsale.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.answer.petsale.annotation.AuthCheck;
import com.answer.petsale.common.BaseResponse;
import com.answer.petsale.common.DeleteRequest;
import com.answer.petsale.common.ErrorCode;
import com.answer.petsale.common.ResultUtils;
import com.answer.petsale.constant.CommonConstant;
import com.answer.petsale.exception.BusinessException;
import com.answer.petsale.model.dto.animal.AnimalAddRequest;
import com.answer.petsale.model.dto.animal.AnimalQueryRequest;
import com.answer.petsale.model.dto.animal.AnimalUpdateRequest;
import com.answer.petsale.model.entity.Animal;
import com.answer.petsale.model.entity.User;
import com.answer.petsale.service.AnimalService;
import com.answer.petsale.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 帖子接口
 *
 */
@RestController
@RequestMapping("/animal")
@Slf4j
public class AnimalController {

    @Resource
    private AnimalService animalService;

    @Resource
    private UserService userService;

    // region 增删改查

    /**
     * 创建
     *
     * @param animalAddRequest
     * @param request
     * @return
     */
    @PostMapping("/add")
    public BaseResponse<Long> addAnimal(@RequestBody AnimalAddRequest animalAddRequest, HttpServletRequest request) {
        if (animalAddRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Animal animal = new Animal();
        BeanUtils.copyProperties(animalAddRequest, animal);

        // 校验 (建议取消注释)
        animalService.validAnimal(animal, true);

        User loginUser = userService.getLoginUser(request);
        animal.setUserId(loginUser.getId());

        boolean result = animalService.save(animal);
        if (!result) {
            throw new BusinessException(ErrorCode.OPERATION_ERROR);
        }
        long newAnimalId = animal.getId();
        return ResultUtils.success(newAnimalId);
    }
    /**
     * 删除
     *
     * @param deleteRequest
     * @param request
     * @return
     */
    @PostMapping("/delete")
    public BaseResponse<Boolean> deleteAnimal(@RequestBody DeleteRequest deleteRequest, HttpServletRequest request) {
        if (deleteRequest == null || deleteRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        User user = userService.getLoginUser(request);
        long id = deleteRequest.getId();
        // 判断是否存在
        Animal oldAnimal = animalService.getById(id);
        if (oldAnimal == null||oldAnimal.getUserId()==null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }
        boolean test = !oldAnimal.getUserId().equals(user.getId()) && !userService.isAdmin(request);
        System.out.println(test);
        // 仅本人或管理员可删除
        if (!oldAnimal.getUserId().equals(user.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }
        boolean b = animalService.removeById(id);
        return ResultUtils.success(b);
    }

    /**
     * 更新
     *
     * @param animalUpdateRequest
     * @param request
     * @return
     */
    @PostMapping("/update")
    @AuthCheck(mustRole = "admin")
    public BaseResponse<Boolean> updateAnimal(@RequestBody AnimalUpdateRequest animalUpdateRequest,
                                              HttpServletRequest request) {
        if (animalUpdateRequest == null || animalUpdateRequest.getId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        // 1. 先从数据库查询旧数据
        long id = animalUpdateRequest.getId();
        Animal oldAnimal = animalService.getById(id);
        if (oldAnimal == null) {
            throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
        }

        // 2. 仅本人或管理员可修改
        User loginUser = userService.getLoginUser(request);
        if (!oldAnimal.getUserId().equals(loginUser.getId()) && !userService.isAdmin(request)) {
            throw new BusinessException(ErrorCode.NO_AUTH_ERROR);
        }

        // 3. 将请求中的新值覆盖到旧数据上
        BeanUtils.copyProperties(animalUpdateRequest, oldAnimal);

        // 4. 参数校验 (可选，但推荐)
        animalService.validAnimal(oldAnimal, false);

        // 5. 将更新后的完整对象保存回数据库
        boolean result = animalService.updateById(oldAnimal);
        return ResultUtils.success(result);
    }

    /**
     * 根据 id 获取
     *
     * @param id
     * @return
     */
    @GetMapping("/get")
    public BaseResponse<Animal> getAnimalById(long id) {
        if (id <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Animal animal = animalService.getById(id);
        return ResultUtils.success(animal);
    }

    /**
     * 获取列表
     *
     * @param animalQueryRequest
     * @return
     */
    @GetMapping("/list")
    public BaseResponse<List<Animal>> listAnimal(AnimalQueryRequest animalQueryRequest) {
        Animal animalQuery = new Animal();
        if (animalQueryRequest != null) {
            BeanUtils.copyProperties(animalQueryRequest, animalQuery);
        }
        QueryWrapper<Animal> queryWrapper = new QueryWrapper<>(animalQuery);
        List<Animal> animalList = animalService.list(queryWrapper);
        return ResultUtils.success(animalList);
    }

    /**
     * 分页获取列表
     *
     * @param animalQueryRequest
     * @param request
     * @return
     */
    /**
     * 分页获取列表
     *
     * @param animalQueryRequest
     * @param request
     * @return
     */
    @PostMapping("/list/page")
    public BaseResponse<Page<Animal>> listAnimalByPage(@RequestBody AnimalQueryRequest animalQueryRequest, HttpServletRequest request) {
        if (animalQueryRequest == null) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        Animal animalQuery = new Animal();
        BeanUtils.copyProperties(animalQueryRequest, animalQuery);
        long current = animalQueryRequest.getCurrent();
        long size = animalQueryRequest.getPageSize();
        String sortField = animalQueryRequest.getSortField();
        String sortOrder = animalQueryRequest.getSortOrder();

        // 限制爬虫
        if (size > 50) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }
        QueryWrapper<Animal> queryWrapper = new QueryWrapper<>(animalQuery);
        // queryWrapper.like(StringUtils.isNotBlank(content), "content", content);
        queryWrapper.orderBy(StringUtils.isNotBlank(sortField),
                sortOrder.equals(CommonConstant.SORT_ORDER_ASC), sortField);
        Page<Animal> animalPage = animalService.page(new Page<>(current, size), queryWrapper);
        return ResultUtils.success(animalPage);
    }
    // endregion

}